package com.example.rbacmanage.service.system.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.entity.system.Menu;
import com.example.rbacmanage.mapper.system.MenuMapper;
import com.example.rbacmanage.mapper.system.RoleMenuMapper;
import com.example.rbacmanage.service.system.MenuService;
import com.example.rbacmanage.vo.MenuTreeResp;
import com.example.rbacmanage.vo.MyUserDetails;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public void save(Menu menu) {
        //查询出父级的等级，在父级的基础上加1
        if (menu.getParentId() != null){
            Menu parent = menuMapper.queryOneById(menu.getParentId());
            if (parent != null){
                menu.setLevel(parent.getLevel()+1);
            }else{
                menu.setLevel(0);
            }
        }
        menuMapper.insert(menu);
    }

    @Override
    public void remove(Long[] ids) {
        for(Long id:ids){
            menuMapper.del(id);
            //删除角色菜单关系表
            roleMenuMapper.deleteByMenuId(id);
        }
    }

    @Override
    public void update(Menu menu) {
        //查询出父级的等级，在父级的基础上加1
        if (menu.getParentId() != null){
            Menu parent = menuMapper.queryOneById(menu.getParentId());
            if (parent != null){
                menu.setLevel(parent.getLevel()+1);
            }else{
                menu.setLevel(0);
            }
        }
        menuMapper.update(menu);
    }

    @Override
    public List<Menu> getAll(Menu menu) {
        MyUserDetails myUserDetails = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (myUserDetails.getId().equals(1)){
            return menuMapper.list(menu);
        }
        menu.getParams().put("userId",myUserDetails.getId());
        return menuMapper.listByUserId(menu);
    }

    @Override
    public PageInfo<Menu> getPage(Menu menu) {
        return new PageInfo<>(menuMapper.list(menu));
    }

    @Override
    public Menu getById(Long id) {
        return menuMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(Menu menu) {
        return menuMapper.count(menu);
    }

    @Override
    public List<MenuTreeResp> getAllTree(Menu menu) {
        List<Menu> menus = menuMapper.list(menu);
        List<MenuTreeResp> menuTreeResps = new ArrayList<>();
        menus.forEach(m -> {
            MenuTreeResp menuTreeResp = new MenuTreeResp();
            menuTreeResp.setLabel(m.getMenuName());
            menuTreeResp.setId(m.getId());
            menuTreeResp.setParentId(m.getParentId());
            menuTreeResp.setValue(m.getId());
            menuTreeResp.setMenuName(m.getMenuName());
            menuTreeResp.setMenuStatus(m.getMenuStatus());
            menuTreeResp.setSort(m.getSort());
            menuTreeResp.setMenuIcon(m.getMenuIcon());
            menuTreeResp.setPermissionCode(m.getPermissionCode());
            menuTreeResp.setMenuPath(m.getMenuPath());
            menuTreeResp.setCreateTime(m.getCreateTime());
            menuTreeResp.setLevel(m.getLevel());
            menuTreeResp.setLink(m.getLink());
            menuTreeResps.add(menuTreeResp);
        });
        if(!StringUtils.isEmpty(menu.getMenuName()) || !StringUtils.isEmpty(menu.getMenuStatus())){
            return menuTreeResps;
        }
        //将list转换成树结构
        List<MenuTreeResp> menulist = new ArrayList<>();
        for (MenuTreeResp menuInfo: menuTreeResps){
            menuInfo.setChildren(getChildren(menuInfo,menuTreeResps));
            if (menuInfo.getParentId()==0L){
                menulist.add(menuInfo);
            }
        }
        return menulist;
    }

    private List<MenuTreeResp> getChildren(MenuTreeResp menuInfo, List<MenuTreeResp> menus){
        List<MenuTreeResp> children = menus.stream().filter(menu -> menu.getParentId().equals(menuInfo.getId())).map(menu -> {
            menu.setChildren(getChildren(menu,menus));
            return menu;
        }).toList();
        if (children.size() > 0){
            return children;
        }
        return List.of();
    }
}
